from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
from sklearn.metrics import silhouette_score, calinski_harabasz_score

# 制造聚类的散点
x, y = make_blobs(n_samples=1000,
                  n_features=2,
                  centers=[[-1,-1],[0,0],[1,1],[2,2]],
                  cluster_std=[0.4,0.2,0.2,0.2],
                  random_state=22)
ch_list = []
for k in range(2,100):
    model = KMeans(n_clusters=k)
    model.fit(x)
    y_pre = model.predict(x)
    # sc_list.append(silhouette_score(x,y_pre)) # sc
    ch_list.append(calinski_harabasz_score(x, y_pre))

plt.figure(figsize=(15,8),dpi=80)
plt.plot(range(2,100),ch_list,marker="o")
plt.show()